/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.semantic;


import alloy.ast.Node;
import alloy.util.Dbg;
import alloy.util.Msg;

/**
 * This class formats error messages and prints them to the Dbg.user
 * buffer.  It also keeps track of whether any error messages have been
 * printed out or not.
 **/
class ErrorReporter {
    
    /** a flag that keeps track of whether any error messages
     were generated or not **/
    private boolean _called = false;

    /** a flag that determines whether we will report the error or not (using
     *  Dbg.user)**/
    private boolean _report = true;
    
    public ErrorReporter() {}
    
    public ErrorReporter(boolean report_) {
        _report = report_; 
    }
    
    /**
     * @return true if this ErrorReporter reports error messages through
     * the Dbg.user method; false if no error messages are ever reported
     */
    public boolean getReport() {
        return _report;
    }
    
    /**
     * Clear or sets the error flag of this.
     **/
    public void setError(boolean error) {
        _called = error;
    }

    /**
     * @return true if this ErrorReporter was used to report an error since
     * the last setError operation
     **/
    public boolean getError() {
        return _called;
    }

    /**
     * Prints the given error message to the Dbg.user buffer and associates
     * it with the given node.  The format of the printed message is "<errMsg>"
     * @param errMsg the error message
     * @param node the node where the error ocurred
     **/
    public void error(String errMsg, Node node) {
        if (_report) Dbg.user(new Msg(errMsg, node));
        _called = true;
    }

    /**
     * Prints the given error message to the Dbg.user buffer and associates
     * it with the given node.  The format of the printed message is
     * "Syntax Error: <errMsg>"
     * @param errMsg the error message
     * @param node the node where the error ocurred
     **/
    public void syntax(String errMsg, Node node) {
        error("Syntax error: " + errMsg, node);
    }

    /**
     * Prints the given error message to the Dbg.user buffer and associates
     * it with the given node.  The format of the printed message is
     * "Semantic Error: <errMsg>"
     * @param errMsg the error message
     * @param node the node where the error ocurred
     **/
    public void semantic(String errMsg, Node node) {
        error("Semantic error: " + errMsg, node);
    }
}
